<html>
<head><meta charset="utf-8"><title>catch_unwind doesn&#x27;t capture panic information · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html">catch_unwind doesn&#x27;t capture panic information</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209242677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209242677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209242677">(Sep 06 2020 at 21:36)</a>:</h4>
<p>So I have some code like this:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">catch_result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">panic</span>::<span class="n">catch_unwind</span><span class="p">(</span><span class="n">curses_main</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Err</span><span class="p">(</span><span class="n">cause</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">catch_result</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{:?}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">cause</span><span class="p">)</span><span class="w"></span>
<span class="w">  </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>


<span class="k">fn</span> <span class="nf">curses_main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">win</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Curses</span>::<span class="n">init</span><span class="p">();</span><span class="w"></span>
<span class="w">  </span><span class="n">panic</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;test panic&quot;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>And when it prints, I just see "Any", I don't see the actual info of the panic message or the line that had the panic or anything else.</p>
<p>Here's the problem: when the terminal is in curses mode, all use of stderr / stdout is suppressed, so I <em>need</em> the panic info to survive until after curses mode is shut down and the terminal goes back to shell mode. Otherwise you don't get any panic info at all.</p>



<a name="209244154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209244154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209244154">(Sep 06 2020 at 22:20)</a>:</h4>
<p>The <code>Any</code> _is_ the panic info, you need to downcast it.</p>



<a name="209244160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209244160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209244160">(Sep 06 2020 at 22:21)</a>:</h4>
<p>or more specifically its the arguments supplied to the <code>panic!</code> call, potentially formatted.</p>



<a name="209244166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209244166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209244166">(Sep 06 2020 at 22:21)</a>:</h4>
<p>So it could be a <code>String</code>, but it could also be an <code>i32</code> for e.g. <code>panic!(42)</code>.</p>



<a name="209244447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209244447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209244447">(Sep 06 2020 at 22:29)</a>:</h4>
<p>So... without knowing what type went into the panic, i'm possibly stuck? I guess it's time for a custom panic hook.</p>



<a name="209244749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209244749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209244749">(Sep 06 2020 at 22:38)</a>:</h4>
<p>Custom panic hook doesn’t really give you anything more than the <code>Any</code>, just a location I guess.</p>



<a name="209244817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209244817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209244817">(Sep 06 2020 at 22:40)</a>:</h4>
<p>(the <code>message</code> being unstable)</p>



<a name="209244834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209244834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209244834">(Sep 06 2020 at 22:41)</a>:</h4>
<p>In most cases downcasting as string <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=f6611103aec5a0fc54377bf2385071cf">would likely serve you well</a></p>



<a name="209244957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209244957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209244957">(Sep 06 2020 at 22:44)</a>:</h4>
<p>custom panic hook lets me manually turn off curses before printing the <del>message</del> PanicInfo.</p>



<a name="209245840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209245840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209245840">(Sep 06 2020 at 23:10)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> FWIW std doesn't do anything different - it also just casts to string and str and then gives up</p>



<a name="209245855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209245855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209245855">(Sep 06 2020 at 23:11)</a>:</h4>
<p>Does the cause have the panic info bundled with it? or is it just the value? Because without the panic info there's a lot less value.</p>



<a name="209245906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209245906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209245906">(Sep 06 2020 at 23:12)</a>:</h4>
<p>You mean the return value of catch_unwind? Just the value</p>



<a name="209245972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/catch_unwind%20doesn%27t%20capture%20panic%20information/near/209245972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/catch_unwind.20doesn&#x27;t.20capture.20panic.20information.html#209245972">(Sep 06 2020 at 23:14)</a>:</h4>
<p>ah, yeah, gotta get that info too. custom hook for sure</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>